package com.example.xpvideo.service.serviceImpl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.xpvideo.context.PageContext;
import com.example.xpvideo.entity.Discover;
import com.example.xpvideo.entity.PageResult;
import com.example.xpvideo.entity.ResponseResult;
import com.example.xpvideo.entity.Video;
import com.example.xpvideo.entity.req.PhpVideoReq;
import com.example.xpvideo.entity.req.UpdateReq;
import com.example.xpvideo.entity.res.PhpDiscoverRes;
import com.example.xpvideo.entity.res.PhpVideoRes;
import com.example.xpvideo.enums.ErrCodeEnum;
import com.example.xpvideo.exception.ErrorException;
import com.example.xpvideo.mapper.DiscoverMapper;
import com.example.xpvideo.mapper.PhpDiscoverMapper;
import com.example.xpvideo.service.PhpDiscoverService;
import com.example.xpvideo.service.TimeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author : 洪俊新
 * @date : 2021/9/14 18:24
 * @description:
 */
@Service
public class PhpDiscoverServiceImpl implements PhpDiscoverService {
    @Autowired
    private PhpDiscoverMapper phpDiscoverMapper;
    @Autowired
    private DiscoverMapper discoverMapper;
    @Autowired
    private TimeService timeService;

    @Override
    public ResponseResult<PageResult<List<PhpDiscoverRes>>> list(PhpVideoReq phpVideoReq) throws ErrorException {

        int page = PageContext.getPage();
        int size = PageContext.getSize();
        String title = phpVideoReq.getTitle();
        String phone = phpVideoReq.getPhone();
        Integer userType = phpVideoReq.getUserType();
        String nickname = phpVideoReq.getNickname();

        PageHelper.startPage(page, size);
        List<PhpDiscoverRes> list = phpDiscoverMapper.discoverList(Wrappers.<Discover>query()
                .like(!ObjectUtils.isEmpty(title), "title", title)
                .eq(!ObjectUtils.isEmpty(phone), "d.phone", phone)
                .eq(!ObjectUtils.isEmpty(userType), "user_type", userType)
                .like(!ObjectUtils.isEmpty(nickname), "nickname", nickname));

        PageInfo pageInfo = new PageInfo<>(list);
        //当前页
        int pageNum = pageInfo.getPageNum();
        //总页数
        int pages = pageInfo.getPages();
        //当前页展示的数量
        int pageSize = pageInfo.getSize();
        //查询总数
        long count = pageInfo.getTotal();
        PageResult<List<PhpDiscoverRes>> listPageResult = new PageResult<>(count,pages,
                pageSize, pageNum, list);

        return ResponseResult.responseResult(0, listPageResult);

    }

    @Override
    public ResponseResult<Object> delete(UpdateReq updateReq) throws ErrorException {
        Discover discover = new Discover();
        discover.setId(updateReq.getId());
        discover.setIsDel(updateReq.getStage());
        discover.setUpdateTime(timeService.getTime());
        int res = discoverMapper.updateDiscover(discover);

        if(res > 0){
            return ResponseResult.responseResult(0,"发现文章删除成功");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20003);
    }

}
